package net.hvacapp.cmvrfforandroid.utils;

import java.text.DecimalFormat;

/**
 * 数字格式化
 */
public class NumberUtil {

    static String[] units = {"", "十", "百", "千", "万", "十万", "百万", "千万", "亿",
            "十亿", "百亿", "千亿", "万亿"};
    static char[] numArray = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};

    /**
     * 格式化结果
     *
     * @param obj
     * @return String
     */
    public static String getTrafficValue(Object obj) {
        if (obj instanceof Integer) {
            Integer value = Integer.valueOf(obj.toString());
            if (value > 100000000) {
                value = value / 10000;
                return value + "W";
            } else if (value == -1) {
                return "--";
            } else {
                return obj.toString();
            }
        } else if (obj instanceof Double) {
            return formatDoublePercent(Double.valueOf(obj.toString()));
        } else {
            return obj.toString();
        }
    }

    public static String formatInt(int value) {
        if (value > 100000000) {
            value = value / 10000;
            return value + "W";
        } else if (value == -1) {
            return "--";
        } else {
            return "" + value;
        }
    }

    public static String formateMoney(double value) {
        DecimalFormat format = new DecimalFormat("￥#0.##元");
        return format.format(value);
    }

    public static String formateDouble(double number) {
        if (number == -1d) {
            return "--";
        }
        DecimalFormat format = new DecimalFormat("#0.##");
        return format.format(number);
    }

    public static String formateDouble1(int number) {
        DecimalFormat format = new DecimalFormat("###");
        return format.format(number);
    }

    /**
     * 动态页 带 “元” 格式
     *
     * @param number
     * @return
     */
    public static String formateDoubleDynamic(double number) {
        if (number == -1d) {
            return "--";
        }
        DecimalFormat format = new DecimalFormat("#0.##");
        return format.format(number)+"元";
    }

    public static String formatDoublePercent(double number) {
        if (number == -1d) {
            return "--";
        }
        DecimalFormat format = new DecimalFormat("#0.##");
        return format.format(number*100);
    }

    /**
     * 格式化整数到汉字
     *
     * @param num
     * @return
     */
    public static String formatNum2Char(int num) {
        char[] val = String.valueOf(num).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            String m = val[i] + "";
            int n = Integer.valueOf(m);
            boolean isZero = n == 0;
            String unit = units[(len - 1) - i];
            if (isZero) {
//				if ('0' == val[i]) {
//					// not need process if the last digital bits is 0
//					continue;
//				} else {
                // no unit for 0
                sb.append(numArray[n]);
//				}
            } else {
                sb.append(numArray[n]);
                sb.append(unit);
            }
        }
        return sb.toString();
    }

    /**
     * 格式化小数点到汉字
     *
     * @param decimal
     * @return
     */
    public static String formatDouble2Char(double decimal) {
        String decimals = String.valueOf(decimal);
        int decIndex = decimals.indexOf(".");
        int integ = Integer.valueOf(decimals.substring(0, decIndex));
        int dec = Integer.valueOf(decimals.substring(decIndex + 1));
        String result = formatNum2Char(integ) + "." + formatFractionalPart(dec);
        return result;
    }

    private static String formatFractionalPart(int decimal) {
        char[] val = String.valueOf(decimal).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int n = Integer.valueOf(val[i] + "");
            sb.append(numArray[n]);
        }
        return sb.toString();
    }
}
